<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/27
 * Time: 14:26
 */

namespace Controller;

class IndexController extends TooController {
    //首页
    public function index(){
        $model=$this->pdo;

        //栏目和商品信息
        $larr=$model->pdo_sel("select * from s_lm order by sort desc",2);
        foreach ($larr as &$v){
            $v['goods']=$model->pdo_sel("select * from s_goods where l_id = '{$v['id']}' and start =1 limit 5",2);
        }
        $this->view->assign('larr',$larr);

        $this->view->display();
    }

    public function lis(){
        //根据不同的入口查询不同的数据
        $where='where start=1 and';
        $url='/index.php?C=Index&F=lis';
        if($_GET['fid']){
            $where.=" f_id = '{$_GET['fid']}' and";
            $url.='&fid='.$_GET['fid'];
        }
        if($_GET['lid']){
            $where.=" l_id = '{$_GET['lid']}' and";
            $url.='&lid='.$_GET['lid'];
        }
        if($_GET['key']){
            $where.=" name like '%{$_GET['key']}%' and";
            $url.='&key='.$_GET['key'];
        }
        $this->view->assign('url',$url);

        $where=trim($where,'and');



        //拼接排序sql,传递两个参数,一个参数代表 字段,一个参数代表排序规则
        if($_GET['order']){
            $order=" order by {$_GET['order']} {$_GET['otype']}";
        }else{
            $order='';
        }


        /**实现分页***/
        $page=$_GET['page']?$_GET['page']:1;
        $limit=2;
        $ofset=($page-1)*$limit;

        $con=$this->pdo->pdo_sel("select count(*) as co from s_goods $where ");
        $con=ceil($con['co']/$limit);//总页数

        $syy=$page-1>0?$page-1:1;
        $xyy=$page+1<$con?$page+1:$con;
        $this->view->assign('page',array('syy'=>$syy,'xyy'=>$xyy,'zyy'=>$con));

        $sql="select * from s_goods $where $order limit $ofset,$limit";

        $goods=$this->pdo->pdo_sel($sql,2);
        $this->view->assign('goods',$goods);
        //分类数据
        $farr=$this->pdo->pdo_sel("select * from s_class",2);
        $this->view->assign('carr',$farr);



        $this->view->display();
    }

    //商品详情页面
    public function goods(){
        $id=$_GET['id'];
        //商品数据
        $sql="select g.*,c.title from s_goods as g join s_class as c on g.f_id=c.id where g.id = $id";
        $goods=$this->pdo->pdo_sel($sql);

        $goods['img']=explode('|',$goods['img']);//图片
        $goods['txt']=htmlspecialchars_decode($goods['txt']);//详细信息
        $this->view->assign('goods',$goods);

        //规格数据
        $goods_g=$this->pdo->pdo_sel("select * from s_goods_g where g_id = $id",2);
        $this->view->assign('goods_g',$goods_g);

        //商品喜欢人数
        $like=$this->pdo->pdo_sel("select count(*) as co from s_like where g_id = '$id'");
        $this->view->assign('like',$like);

        //判断我有没有进入喜欢
        if($_SESSION['Home']['Member']['id']){
            $mlike=$this->pdo->pdo_sel("select count(*) as co from s_like where g_id = $id and m_id = {$_SESSION['Home']['Member']['id']}");
            $mlike=$mlike['co'];
        }else{
            $mlike=-1;
        }
        $this->view->assign('mlike',$mlike);

        $this->view->display();
    }

    //加入喜欢/收藏
    public function likeadd(){
        $mlike=$_GET['mlike'];
        if($mlike>0){
            //删除
            if($this->pdo->pdo_del('s_like'," m_id = {$_SESSION['Home']['Member']['id']} and g_id = '{$_GET['gid']}'")){
                $this->retuAjax(array('msg'=>'取消成功','type'=>1));
            }else{
                $this->retuAjax(array('msg'=>'服务器错误','type'=>2));
            }
        }else{
            //添加
            $arr=array();
            $arr['g_id']=$_GET['gid'];
            $arr['m_id']=$_SESSION['Home']['Member']['id'];
            $arr['time']=date('Y-m-d H:i:s',time());
            if($this->pdo->pdo_ins($arr,'s_like')){
                $this->retuAjax(array('msg'=>'加入成功','type'=>1));
            }else{
                $this->retuAjax(array('msg'=>'服务器错误','type'=>2));
            }
        }

    }

    //加入购物车
    public function flow(){
        $arr=array();
        $arr['g_id']=$_GET['gid'];
        $arr['gg_id']=$_GET['did'];
        $arr['num']=$_GET['num'];
        $arr['m_id']=$_SESSION['Home']['Member']['id'];
        $arr['time']=date('Y-m-d H:i:s');

        //判断是否有,如果有就加入对应的数量
        $cont=$this->pdo->pdo_sel("select * from s_flow where m_id ='{$arr['m_id']}' and g_id = '{$arr['g_id']}' and gg_id = '{$arr['gg_id']}'");
        if(!empty($cont['id'])){
            //修改数量
            if($this->pdo->pdo_upd(array('num'=>$arr['num']+$cont['num']),'s_flow', " id = '{$cont['id']}'")){
                $this->retuAjax(array('msg'=>'添加成功','type'=>1));
            }else{
                $this->retuAjax(array('msg'=>'服务器错误','type'=>2));
            }
        }else{
            //插入数据
            if($this->pdo->pdo_ins($arr,'s_flow')){
                $this->retuAjax(array('msg'=>'添加成功','type'=>1));
            }else{
                $this->retuAjax(array('msg'=>'服务器错误','type'=>2));
            }
        }
    }
}